package interceptcrossing.ui;

import interceptcrossing.Intercept;
import interceptcrossing.Intersection;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class InterceptPanel extends JPanel implements MainWindowDirector.Listener {

	private JTable interceptTable;
	private DefaultTableModel interceptTableModel;
	private List<Intercept> intercepts;
	private MainWindowDirector windowDirector;

	public InterceptPanel(MainWindowDirector windowDirector) {
		this.windowDirector = windowDirector;

		this.intercepts = new LinkedList<Intercept>();
		this.intercepts.addAll(windowDirector.getIntercepts());

		this.interceptTableModel = new DefaultTableModel(new String[][]{},
			new String[]{"№", "X1",
				"Y1",
				"X2",
				"Y2"});
		this.interceptTable = new JTable(this.interceptTableModel);
		this.interceptTable.setRowSelectionAllowed(true);
		this.interceptTable.setColumnSelectionAllowed(false);
		this.interceptTable.setFillsViewportHeight(true);

		for (Intercept intercept : intercepts) {
			this.interceptAdded(intercept);
		}

		this.windowDirector.addListener(this);

		super.setLayout(new GridLayout(1, 1));
		super.add(new JScrollPane(this.interceptTable));

		this.interceptTable.getSelectionModel().addListSelectionListener(
			new ListSelectionListener() {

				public void valueChanged(ListSelectionEvent e) {
					int index = interceptTable.getSelectionModel().getMinSelectionIndex();
					InterceptPanel.this.windowDirector.changeCurrentIntercept(index);
				}
			});
	}

	public void interceptAdded(Intercept intercept) {
		this.intercepts.add(intercept);
		this.interceptTableModel.addRow(new String[]{
			Integer.toString(this.interceptTableModel.getRowCount() + 1),
			Double.toString(intercept.begin().x()), 
			Double.toString(intercept.begin().y()),
			Double.toString(intercept.end().x()),
			Double.toString(intercept.end().y()),
		});
	}

	public void interceptRemoved(Intercept intercept) {
		int rowIndex = this.intercepts.indexOf(intercept);
		this.intercepts.remove(intercept);

		assert (rowIndex >= 0);

		this.interceptTableModel.removeRow(rowIndex);

		for(int i = 0; i < this.interceptTableModel.getRowCount(); i++) {
			this.interceptTableModel.setValueAt(Integer.toString(i + 1), i, 0);
		}
	}

	public void interceptsCleared() {
		this.intercepts.clear();

		int rowCount = this.interceptTableModel.getRowCount();

		for (int i = rowCount - 1; i >= 0; i--) {
			this.interceptTableModel.removeRow(i);
		}
	}

	public void currentInterceptChanged(int index, Intercept intercept) {
		if (index == -1 || intercept == null) {
			this.interceptTable.getSelectionModel().clearSelection();
		} else {
			this.interceptTable.getSelectionModel().setSelectionInterval(index, index);
		}
	}

	public void intersectionsFound(Set<Intersection> intersections) {
		//
	}
}
